// pages/compileCard/compileCard.js
var tuiwa = require("../../tuiwa.js")
Page({
  data: {
    value: "",
    nickName: "",
    userName: "",
    avatarUrl: "",
    user_avatarUrl: "",
    list: [],
    showModal: false,
    luyin: 0,
    frameNum: 0,
    src: "",
    recorderManager: '',
    t_length: 0,
    is_true: 0,
    images: '',
  },
  onLoad: function (query) {
    let _this = this
    // tt.getUserInfo({
    //   withCredentials: true,
    //   success(res) {
    //     _this.setData({
    //       userName: res.userInfo.nickName,
    //       user_avatarUrl: res.userInfo.avatarUrl
    //     })
    //     console.log(_this.data.userName)
    //   },
    //   fail(res) {
    //     console.log("getUserInfo 调用失败");
    //   },
    // });
    let list = JSON.parse(query.list)
    this.setData({
      value: query.list_value,
      t_length: query.list_value.length,
      images: query.images,
      is_true: query.is_true,
      width: query.width,
      height: query.height,
      left: query.left,
      top: query.top,
      num: query.num,
      list: list,
    });
    // console.log(this.data.width, this.data.height, this.data.left, this.data.top, this.data.num, this.data.images)
  },
  // 完成
  save: function (e) {
    this.setData({
      area_value: this.data.value
    })
    let that = this
    tt.request({
      url: 'https://vs.tuiwa.com/dy_mini/check_content',
      method: "post",
      header: {
        "content-type": "application/json"
      },
      data: {
        appId: tuiwa.globalData.appId,
        area_value: that.data.area_value
      },
      success: (res) => {
        console.log(res)
        if (!res.data.data) {
          // var area_value = this.data.area_value
          // var textareaContent = this.data.textareaContent
          // tt.navigateTo({
          //   url: `/pages/newCard_compile/newCard_compile?area_value=${area_value}&textareaContent=${textareaContent}`
          // });
          tt.navigateTo({
            url: '../../pages/newCard_compile/newCard_compile?area_value=' + that.data.area_value
              + '&is_true=' + that.data.is_true
              + '&images=' + that.data.images
              + '&width=' + that.data.width
              + '&height=' + that.data.height
              + '&left=' + that.data.left
              + '&top=' + that.data.top
              + '&num=' + that.data.num,
          });
        } else {
          tt.showToast({
            title: "敏感词请重新输入！",
            icon: "none"
          });
          return
        }
      }
    });
  },
  // 限制字数 
  bindText: function (e) {
    var length = e.detail.value.length;
    var value = e.detail.value;
    this.setData({
      t_length: length,
      value: value
    })
    // console.log('111', this.data.value)
  },

  // 选择祝福语
  onList: function () {
    let list = JSON.stringify(this.data.list)
    tt.navigateTo({
      url: '../../pages/choose_list/choose_list?list=' + list
        + '&images=' + this.data.images
        + '&is_true=' + this.data.is_true
        + '&width=' + this.data.width
        + '&height=' + this.data.height
        + '&left=' + this.data.left
        + '&top=' + this.data.top
        + '&num=' + this.data.num,
    });
  },
  // 返回
  back: function () {
    tt.navigateBack({
      delta: 1
    });
  },

  // 更换头像
  headimage: function () {
    console.log('更换头像')
    var _this = this;
    tt.chooseImage({
      count: 1, // 默认9      
      sizeType: ['original', 'compressed'],
      // 指定是原图还是压缩图，默认两个都有      
      sourceType: ['album', 'camera'],
      // 指定来源是相册还是相机，默认两个都有    
      success: function (res) {
        console.log(res)
        // 返回选定照片的本地文件路径tempFilePath可以作为img标签的src属性显示图片 
        //然后请求接口把图片传给后端存到服务器即可       
        _this.setData({
          user_avatarUrl: res.tempFilePaths
        })
      }
    })
  },
  // 修改
  toRevise: function () {
    console.log('修改')
    this.setData({
      is_true: 1,
      nickName: this.data.userName
    })
  },
  toSure: function () {
    console.log('确定')
    this.setData({
      is_true: 2,
    })
  },
  onTextInput: function (e) {
    console.log('onTextInput', e)
    this.setData({
      userName: e.detail.value,
    });
  },
  // 开始录音
  start: function () {
    let that = this;
    console.log('开始录音')
    this.setData({
      luyin: 1
    })
    const options = {
      duration: 60000, // 录音自动完成时长，单位 ms
      sampleRate: 12000, // 采样率
      numberOfChannels: 1, // 录音通道数
      encodeBitRate: 25000, // 码率
      // 帧大小，单位 KB。
      // 如果设置了值，那么每当录音内容达到帧大小时会通过 onFrameRecorded 返回内容
      frameSize: 100,
    };
    record.recorderManager.start(options);
    record.recorderManager.onFrameRecorded((frame) => {
      console.log(frame, 'frame')
      let { isLastFrame } = frame;
      this.setData({
        frameNum: this.data.frameNum + 1,
      });
      isLastFrame && tt.showToast({ title: "录音结束" });
    });
  },
  // 结束录音
  stop: function () {
    this.setData({
      luyin: 2
    })
    if (record.recorderManager && !this.innerAudioContext) {
      console.log(record.recorderManager, "21342")
      record.recorderManager.stop();
      console.log('结束录音')
    }
    if (this.innerAudioContext) {
      console.log('结束播放')
    }
  },
  // 播放录音
  play: function () {
    let that = this
    console.log('播放录音')
    this.setData({
      luyin: 3
    })
    this.innerAudioContext = tt.createInnerAudioContext();
    this.innerAudioContext.onError((res) => {
      // 播放音频失败的回调
      console.log(res)
    })
    var source = tt.getStorageSync('videoSource');
    console.log(source, "source")
    console.log(record, "sssssssss")
    this.innerAudioContext.src = source;  // 这里可以是录音的临时路径
    this.innerAudioContext.play()
  },
  // 删除录音
  remove: function () {
    console.log('删除录音')
    this.setData({
      luyin: 0
    })
  },
})